<?php

namespace app\admin\controller;

use think\facade\Session;
use app\admin\controller\Base;
use app\common\model\Article as ArticleModel;
use app\common\model\Category as CategoryModel;

class News extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->Model = new ArticleModel();
        $this->first_category_name = 'news';
        $this->CategoryModel = new CategoryModel();
        $category_tree = $this->CategoryModel->treeView(false);
        if(isset($category_tree[$this->first_category_name])){
          $tree_node = $category_tree[$this->first_category_name];
        }else{
          $tree_node = $this->CategoryModel->emptyTreeRootNode();
        }
        $this->tree_node = $tree_node;
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $param = [];
        $param = $this->request->param();
        $default_param = $this->index_default_param();
        $res = $this->Model->minePaginate(array_merge($param, $default_param));
        if(isset($this->tree_node['children'])){
          $categories = $this->tree_node['children'];
        }else{
          $categories = [];
        }
        if(isset($param['second_category_id'])){
          $second_category_id = intval($param['second_category_id']);
        }else{
          $second_category_id = -1;
        }
        $this->assign("second_category_id", $second_category_id);
        $this->assign("categories", $categories);
        $this->assign("param", $param);
        $this->assign("res", $res);
        return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        if($this->request->isPost()){
            $data = $this->getPostData();
            $res = $this->Model->createOrUpdate($data);
            if($res["status"]){
                Session::flash('success', $res['msg']);
                $this->success($res["msg"], url('index'));
            }else{
                Session::flash('danger', $res['msg']);
                $this->error($res["msg"]);
            }
        }else{
            if(isset($this->tree_node['children'])){
              $categories = $this->tree_node['children'];
            }else{
              $categories = [];
            }
            $article = ["status" => 1, "pc_cover_id" => 0, "sort" => 0, 'is_recommand' => 0, 'second_category_id' => 0];
            $this->assign("article", $article);
            $this->assign("categories", $categories);
            return $this->fetch("form");
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id = 0)
    {
        $id = intval($id);
        if(isset($this->tree_node['id'])){
          $first_category_id = $this->tree_node['id'];
        }else{
          $first_category_id = -1;
        }
        $article = $this->Model->where("id", $id)->where("first_category_id", $first_category_id)->find();
        if(empty($article)){
            Session::flash('danger', '未找到编辑记录');
            $this->redirect('index');
        }

        if($this->request->isPost()){
          $data = $this->getPostData();
          $res = $this->Model->createOrUpdate($data);
          if($res["status"]){
            Session::flash('success', $res['msg']);
            $this->success($res["msg"], url('index'));
          }else{
            Session::flash('danger', $res['msg']);
            $this->error($res["msg"]);
          }
        }else{
          if(isset($this->tree_node['children'])){
            $categories = $this->tree_node['children'];
          }else{
            $categories = [];
          }
          $this->assign("article", $article);
          $this->assign("categories", $categories);
          return $this->fetch("form");
        }
    }

    // /**
    //  * 删除指定资源
    //  *
    //  * @param  int  $id
    //  * @return \think\Response
    //  */
    // public function delete($id = 0)
    // {
    //     if($this->request->isDelete()){
    //         // GroupModel::destroy($id);
    //         $res = $this->Model->deleteTypeItem($first_category_id, $id);
    //         if($res['status']){
    //             Session::flash('success', $res['msg']);
    //             $this->success($res['msg'], url('index'));
    //         }else{
    //             Session::flash('danger', $res['msg']);
    //             $this->error($res['msg'], url('index'));
    //         }

    //     }else{
    //         $this->error('您请求的方法不存在', url('index'));
    //     }
    // }

    protected function index_default_param(){
        $param = [];
        if(empty($this->tree_node)){
            $param['first_category_id'] = 0;
        }else{
            $param['first_category_id'] = $this->tree_node['id'];
        }
        return $param;
    }

    private function getPostData(){
        $fields = [
            ['name' => 'id', 'type' => 'integer', 'is_null' => true],
            ['name' => 'title', 'type' => 'string', 'default' => ''],
            ['name' => 'second_category_id', 'type' => 'integer', 'default' => 0],
            ['name' => 'pc_cover_id', 'type' => 'integer', 'default' => 0],
            ['name' => 'description', 'type' => 'string', 'default' => ''],
            ['name' => 'content', 'type' => 'rich_text', 'default' => ''],
            ['name' => 'seo_description', 'type' => 'string', 'default' => ''],
            ['name' => 'seo_keywords', 'type' => 'string', 'default' => ''],
            // 使用switch开关，所以默认值必须是false，因为switch为关闭状态时返回的值里没有该字段
            ['name' => 'status', 'type' => 'boolean', 'default' => false],
            ['name' => 'is_recommand', 'type' => 'boolean', 'default' => 0],
            ['name' => 'sort', 'type' => 'integer', 'default' => 0],
        ];

        $default = [
            'slug' => '',
            'wap_banner_id' => 0,
            'validate_type' => $this->first_category_name,
            'validate_scene' => 'default',
        ];

        if(empty($this->tree_node)){
            $default['first_category_id'] = 0;
        }else{
            $default['first_category_id'] = $this->tree_node['id'];
        }

        return $this->parsePostParam($fields, $default);
    }
}
